package com.scan.bus.util;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * 时间转换工具
 */
public class TimeUtil {
    public static  final String FORMAT_YYY_MM_DD_HH_MM_EM ="yyyy-MM-dd HH:mm:ss";
    public static  final String FORMAT_YYY_MM_DD_HH_MM ="yyyy年MM月dd日 HH:mm";
    public static  final String FORMAT_MM_DD_HH_MM ="MM月dd日 HH:mm";
    public static  final String FORMAT_YYY_MM_DD ="yyyy-MM-dd";
    public static  final String FORMAT_HH_MM_ss ="HH:mm:ss";
    /**
     * 获取日
     * @param time
     * @param yearTimeFormat
     * @return
     */
    public static int getDdTime(String time, String yearTimeFormat){
        try {
            SimpleDateFormat format = new SimpleDateFormat(yearTimeFormat);
            Calendar cal = Calendar.getInstance();
            cal.setTime(new Date(format.parse(time).getTime()));
            return  cal.get(Calendar.DAY_OF_MONTH);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    }


    public static int getDdCurrentTime(){
        try {
            Calendar c = Calendar.getInstance();
            return  c.getActualMaximum(Calendar.DAY_OF_MONTH);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取月
     * @param time
     * @param yearTimeFormat
     * @return
     */
    public static int getMMTime(String time, String yearTimeFormat){
        try {
            SimpleDateFormat format = new SimpleDateFormat(yearTimeFormat);
            Calendar cal = Calendar.getInstance();
            cal.setTime(new Date(format.parse(time).getTime()));
            return  cal.get(Calendar.MONTH) +1;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return Calendar.getInstance().get(Calendar.MONTH)+1;
    }
    /**
     * 获取年
     * @param time
     * @param yearTimeFormat
     * @return
     */
    public static int getYYTime(String time, String yearTimeFormat){
        try {
            SimpleDateFormat format = new SimpleDateFormat(yearTimeFormat);
            SimpleDateFormat YY = new SimpleDateFormat("yyyy");
            return  Integer.valueOf(YY.format(new Date(format.parse(time).getTime()))) ;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return Calendar.getInstance().get(Calendar.YEAR);
    }
    /**
     * 获取年
     * @return
     */
    public static int getYear() {
        Calendar cd = Calendar.getInstance();
        return cd.get(Calendar.YEAR);
    }
        public static String  getDayTime(long millisecond){
        String str = "";
        long day = millisecond /  86400000;
        if(day >= 0){
            str = day + "天";
        }
        return str;
    }
    /**
     * 当天的显示时间格式
     *
     * @return HH:mm
     */
    public static String getFormatTime(String formatData) {
        SimpleDateFormat format = new SimpleDateFormat(formatData);
        return format.format(new Date().getTime());
    }
    /**
     * 当天的显示时间格式
     *
     * @param time time
     * @return HH:mm
     */
    public static String getYYYYmmddhhmm(long time,String formatData) {
        SimpleDateFormat format = new SimpleDateFormat(formatData);
        return format.format(new Date(time));
    }

    /**
     * 当天的显示时间格式
     *
     * @param time time
     * @return HH:mm
     */
    public static long getLongTime(long time) {
        Calendar cd = Calendar.getInstance();
        cd.setTimeInMillis(Calendar.getInstance().getTimeInMillis()+time);
        return cd.getTimeInMillis();
    }


    /**
     * 当天的显示时间格式
     *
     * @param time time
     * @return HH:mm
     */
    public static String getHourAndMin(long time) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        return format.format(new Date(time));
    }


    /**
     * 当天的显示时间格式
     * @return HH:mm
     */
    public static long getNowTime() {
        Calendar cd = Calendar.getInstance();
        cd.setTimeInMillis(Calendar.getInstance().getTimeInMillis());
        return cd.getTimeInMillis();
    }


    /**
     * 获取时间
     * @param hhmmsss
     * @return
     */
    public static long getHHmmssLong(String hhmmsss){
        try {
            SimpleDateFormat ft = new SimpleDateFormat("HH:mm:ss");
            Date answeredTime = ft.parse(hhmmsss);
            long time = answeredTime.getTime();
            return time;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }




    /**
     * 不同一周的显示时间格式
     *
     * @param time       time
     * @param timeFormat timeFormat
     * @return String
     */
    public static String getTime(long time, String timeFormat) {
        SimpleDateFormat format = new SimpleDateFormat(timeFormat);
        return format.format(new Date(time));
    }

    /**
     * 不同年的显示时间格式
     *
     * @param time           time
     * @param yearTimeFormat yearTimeFormat
     * @return String
     */
    public static String getYearTime(long time, String yearTimeFormat) {
        SimpleDateFormat format = new SimpleDateFormat(yearTimeFormat);
        return format.format(new Date(time));
    }

    /*
     *毫秒转HH:mm:ss"
     */
    public static String timeFormHHmmss(long time) {
        long currentTime =  time - TimeZone.getDefault().getRawOffset();
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");//初始化Formatter的转换格式。

        String hms = formatter.format(currentTime);


        return  hms;
    }
    /*
     *毫秒转HH:mm:ss"
     */
    public static String timeFormHHmmssChina(long time) {
        long currentTime =  time - TimeZone.getDefault().getRawOffset();
        SimpleDateFormat formatter = new SimpleDateFormat("HH时mm分ss秒");//初始化Formatter的转换格式。

        String hms = formatter.format(currentTime);


        return  hms;
    }
    /*
     *毫秒转HH:mm:ss"
     */
    public static String timeFormDdHHmmss(long time) {
        Date date=new Date();
        date.setTime(time);
        SimpleDateFormat formatter;
        if(time>86400*1000) {
             formatter = new SimpleDateFormat("dd天HH:mm:ss");//初始化Formatter的转换格式。
        }else{
            formatter = new SimpleDateFormat("HH:mm:ss");//初始化Formatter的转换格式。
        }

        String hms = formatter.format(date);
        return  hms;
    }
    /*
     *毫秒转HH:mm:ss"
     */
    public static String timeFormmss(long time) {
        long currentTime =  time - TimeZone.getDefault().getRawOffset();
        SimpleDateFormat formatter = new SimpleDateFormat("mm:ss");//初始化Formatter的转换格式。

        String hms = formatter.format(currentTime);


        return  hms;
    }
    /*
     *毫秒转HH:mm:ss"
     */
    public static String timeFormHHmmssUtil(long time) {
        long currentTime =  time - TimeZone.getDefault().getRawOffset();
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");//初始化Formatter的转换格式。
        String hms = formatter.format(currentTime);

        if(!hms.substring(0,2).equals("00")){
            return  hms;
        }else{
            SimpleDateFormat formatterMM = new SimpleDateFormat("mm:ss");//初始化Formatter的转换格式。
            String ms = formatterMM.format(currentTime);
            return  ms;
        }
    }

    /*
     *毫秒转HH:mm:ss"
     */
    public static String timeFormHHmmssUtil(long time,long maxTime) {
        long currentTime =  time - TimeZone.getDefault().getRawOffset();
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");//初始化Formatter的转换格式。
        String hms = formatter.format(currentTime);

        if(!hms.substring(0,2).equals("00")){
            return  hms;
        }else{
            if(maxTime<60*60*1000) {
                SimpleDateFormat formatterMM = new SimpleDateFormat("mm:ss");//初始化Formatter的转换格式。
                String ms = formatterMM.format(currentTime);
                return ms;
            }else{
                return hms;
            }
        }
    }

    /*
     * 毫秒转化时分秒毫秒
     */
    public static String formatDhmsTime(long ms) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;

        StringBuffer sb = new StringBuffer();
        if (day > 0) {
              if(day<10) {
                  sb.append("0"+day + "天");
              }else{
                  sb.append(day + "天");
              }
        }
        if (hour > 0) {
            if(hour<10) {
                sb.append("0"+hour + "小时");
            }else{
                sb.append(hour + "小时");
            }
        }else if(hour==0){
            if(sb.toString().contains("天")){
                sb.append( "00小时");
            }else{
                sb.append( "00小时");
            }
        }
        if (minute > 0) {
            if(minute<10) {
                sb.append("0"+minute + "分");
            }else{
                sb.append(minute + "分");
            }
        }else if(minute ==0){
            if(sb.toString().contains("小时")){
                sb.append( "00分");
            }
        }
        if (second > 0) {
            if(second<10) {
                sb.append("0"+second + "秒");
            }else{
                sb.append(second + "秒");
            }
        }else if(second==0){
            if(sb.toString().contains("分")){
                sb.append( "00秒");
            }
        }
        return sb.toString();
    }

    /*
     * 毫秒转化时分秒毫秒hh:;mm:ss
     */
    public static String formatDhmsTimeData(long ms) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;

        StringBuffer sb = new StringBuffer();
//        if (day > 0) {
//            if(day<10) {
//                sb.append("0"+day + "天");
//            }else{
//                sb.append(day + "天");
//            }
//        }
        hour = day*24+hour;
        if (hour > 0) {
            if(hour<10) {
                sb.append("0"+hour + ":");
            }else{
                sb.append(hour + ":");
            }
        }else if(hour==0){
             sb.append( "00:");
        }
        if (minute > 0) {
            if(minute<10) {
                sb.append("0"+minute + ":");
            }else{
                sb.append(minute + ":");
            }
        }else if(minute ==0){
                sb.append( "00:");
        }
        if (second > 0) {
            if(second<10) {
                sb.append("0"+second + "");
            }else{
                sb.append(second + "");
            }
        }else if(second==0){
                sb.append( "00");
        }
        return sb.toString();
    }


    /*
     * 毫秒转化时分秒毫秒
     */
    public static String formatGoodDhmsTime(long ms) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;

        StringBuffer sb = new StringBuffer();
        if (day > 0) {
            if(day<10) {
                sb.append("0"+day + "天");
            }else{
                sb.append(day + "天");
            }
        }
        if (hour > 0) {
            if(hour<10) {
                sb.append("0"+hour + ":");
            }else{
                sb.append(hour + ":");
            }
        }else if(hour==0){
            if(sb.toString().contains("天")){
                sb.append( "00:");
            }else{
                sb.append( "00:");
            }
        }
        if (minute > 0) {
            if(minute<10) {
                sb.append("0"+minute + ":");
            }else{
                sb.append(minute + ":");
            }
        }else if(minute ==0){
                sb.append( "00:");
        }
        if (second > 0) {
            if(second<10) {
                sb.append("0"+second + "");
            }else{
                sb.append(second + "");
            }
        }else if(second==0){
                sb.append( "00");
        }
        return sb.toString();
    }

    public static String getDddHourAndMin(long seconds) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = seconds / dd;
        Long hour = (seconds - day * dd) / hh;
        Long minute = (seconds - day * dd - hour * hh) / mi;
        Long second = (seconds - day * dd - hour * hh - minute * mi) / ss;

        StringBuffer sb = new StringBuffer();
        if (day > 0) {
            if(day<10) {
                sb.append("0"+day + "天");
            }else{
                sb.append(day + "天");
            }
        }
        if (hour > 0) {
            if(hour<10) {
                sb.append("0"+hour + ":");
            }else{
                sb.append(hour + ":");
            }
        }else if(hour==0){
            sb.append( "00:");
        }
        if (minute > 0) {
            if(minute<10) {
                sb.append("0"+minute + ":");
            }else{
                sb.append(minute + ":");
            }
        }else if(minute ==0){
            sb.append( "00:");
        }
        if (second > 0) {
            if(second<10) {
                sb.append("0"+second + "");
            }else{
                sb.append(second + "");
            }
        }else if(second==0){
            sb.append( "00");
        }
        return sb.toString();
    }

    //计算两个日期相差年数
    public static int yearDateDiff(String endDate){
        Calendar calStartTime = Calendar.getInstance(); //获取日历实例
        int currentYear = getYear();
        calStartTime.setTime(strTodate(endDate)); //字符串按照指定格式转化为日期

        return currentYear - calStartTime .get(Calendar.YEAR);
    }

    public static Date strTodate(String strTodate){
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(strTodate);
        } catch (ParseException e) {

        }
        return date;
    }


}
